package cn.wubo.beetsql.controller;

import cn.wubo.beetsql.entity.DemoManytable;
import cn.wubo.beetsql.entity.DemoOnetoone;
import cn.wubo.beetsql.entity.DemoTable;
import cn.wubo.beetsql.service.IDemoTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/demoTable")
public class DemoTableController {

    @Autowired
    IDemoTableService demoTableService;

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String inserDemoTable(@RequestParam(value = "name", required = true) String name,
                                 @RequestParam(value = "message") String message) {
        DemoTable demoTable = new DemoTable();
        demoTable.setName(name);
        demoTable.setMessage(message);
        int t = demoTableService.add(demoTable);
        if (t == 1) {
            return demoTable.toString();
        } else {
            return "error:insert";
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateDemoTable(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,
                                  @RequestParam(value = "message") String message) {
        DemoTable demoTable = new DemoTable();
        demoTable.setId(id);
        demoTable.setName(name);
        demoTable.setMessage(message);
        int t = demoTableService.update(demoTable);
        if (t == 1) {
            return demoTable.toString();
        } else {
            return "error:update";
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deleteDemoTable(@PathVariable("id") int id) {
        int t = demoTableService.delete(id);
        if (t == 1) {
            return "sucess:delete";
        } else {
            return "error:none";
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public DemoTable getDemoTableById(@PathVariable("id") int id) {
        return demoTableService.getById(id);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<DemoTable> getDemoTables() {
        return demoTableService.getAllList();
    }

    @RequestMapping(value = "/getDemoTableBatman", method = RequestMethod.GET)
    public DemoTable getDemoTableBatman() {
        return demoTableService.getBatman();
    }

    @RequestMapping(value = "/getDemoTableBatmanWithLambda", method = RequestMethod.GET)
    public DemoTable getDemoTableBatmanWithLambda() {
        return demoTableService.getBatmanWithLambda();
    }

    @RequestMapping(value = "/getDemoTableWithOther", method = RequestMethod.GET)
    public Map getDemoTableWithOther(@RequestParam(value = "name") String name) {
        DemoTable demoTable = new DemoTable();
        demoTable.setName(name);
        DemoTable res = demoTableService.getDemoTableWithOther(demoTable).get(0);
        DemoOnetoone demoOnetoone = (DemoOnetoone) res.get("demoOnetoone");
        List<DemoManytable> demoManytables = (List<DemoManytable>) res.get("demoManytable");

        Map r = new HashMap();
        r.put("name",res.getName());
        r.put("One",demoOnetoone);
        r.put("Many",demoManytables);

        return r;
    }
}
